import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yishop/model/OrderModel.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';

class OrderInfoPage extends StatefulWidget{
  Map arguments;
  OrderInfoPage({Key key,this.arguments}):super(key: key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _OrderInfoPageState();
  }

}

class _OrderInfoPageState extends State<OrderInfoPage>{
  Result result;
  String area = "";
  String province = "";//省
  String city="";//市
  String county="";//区
  String detailsAddress = "";//详细地址
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
     result = widget.arguments["result"];
    _getAddress();
  }

  void _getAddress() {
    String allAddress =  result.address;
    try{
      province = allAddress.substring(allAddress.indexOf("p:")+2,allAddress.indexOf("c:"));
      city = allAddress.substring(allAddress.indexOf("c:")+2,allAddress.indexOf("t:"));
      county = allAddress.substring(allAddress.indexOf("t:")+2,allAddress.indexOf("a:"));
      detailsAddress = allAddress.substring(allAddress.indexOf("a:")+2,allAddress.length);
      area = "$province$city$county $detailsAddress";
    }catch(e){

    }

  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: AppBar(title: Text("订单详情")),
      body: Container(
        child: ListView(
          children: <Widget>[
            //收货地址
            Container(
              color: Colors.white,
              child: Column(
                children: <Widget>[
                  SizedBox(height: 10),
                  ListTile(
                    leading: Icon(Icons.location_on_outlined),
                    title: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("${result.name}  ${result.phone}"),
                        SizedBox(height: 10),
                        Text(this.area),
                      ],
                    ),
                  ),
                  SizedBox(height: 10),
                ],
              ),
            ),
            SizedBox(height: 16),
            //列表
            Container(
              color: Colors.white,
              padding: EdgeInsets.all(10),
              child: Column(
                children: this._itemProductWidget(result.orderItem),
              ),
            ),

            //详情信息
            Container(
              color: Colors.white,
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Column(
                children: <Widget>[

                  ListTile(
                    title: Row(
                      children: <Widget>[
                        Text("订单编号:",style: TextStyle(fontWeight: FontWeight.bold)),
                        Text(result.sId)
                      ],
                    ),
                  ),

                  ListTile(
                    title: Row(
                      children: <Widget>[
                        Text("下单日期:",style: TextStyle(fontWeight: FontWeight.bold)),
                        Text("2021-07-22")
                      ],
                    ),
                  ),

                  ListTile(
                    title: Row(
                      children: <Widget>[
                        Text("支付方式:",style: TextStyle(fontWeight: FontWeight.bold)),
                        Text("支付宝支付")
                      ],
                    ),
                  ),

                  ListTile(
                    title: Row(
                      children: <Widget>[
                        Text("配送方式:",style: TextStyle(fontWeight: FontWeight.bold)),
                        Text("顺丰")
                      ],
                    ),
                  )

                ],
              ),
            ),
            SizedBox(height: 16),
            Container(
              color: Colors.white,
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Column(
                children: <Widget>[
                  ListTile(
                      title: Row(
                        children: <Widget>[
                          Text("总金额:",style: TextStyle(fontWeight: FontWeight.bold)),
                          Text("￥${result.allPrice}元",style: TextStyle(
                              color: Colors.red
                          ))
                        ],
                      )
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  List<Widget> _itemProductWidget(orderItems){
    List<Widget> tempList = [];
    for(int i = 0;i<orderItems.length;i++){
      tempList.add(Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
            width: ScreenAdapter.width(120),
            child: Image.network(
                '${orderItems[i].productImg}',
                fit: BoxFit.cover),
          ),
          Expanded(
              flex: 1,
              child: Container(
                padding: EdgeInsets.fromLTRB(10, 10, 10, 5),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text("${orderItems[i].productTitle}",
                        maxLines: 2,
                        style: TextStyle(color: Colors.black54)),
                    ListTile(
                      leading: Text("￥${orderItems[i].productPrice}",
                          style: TextStyle(color: Colors.red)),
                      trailing: Text('x${orderItems[i].productCount}'),
                    )
                  ],
                ),
              ))
        ],
      ));
    }
    return tempList;
  }

}